#coding:utf-8
import requests
import json
import numpy as np
import cv2
import base64
import matplotlib.pyplot as plt
from PIL import Image
import msvcrt
import time

def getByte(path):
    with open(path, 'rb') as f:
        img_byte = base64.b64encode(f.read()) #二进制读取后变base64编码
    img_str = img_byte.decode('ascii') #转成python的unicode
    return img_str 
    
img_str = getByte('./data/images/a.jpeg')

requestsss={'name':'李四', 'image':img_str, 'wait':0}
req = json.dumps(requestsss) #字典数据结构变json(所有程序语言都认识的字符串)

res=requests.post('http://127.0.0.1:6060/', data=req)
print(res.text)

#由于服务器处理需要时间，先让服务器返回success，然后再询问服务器结果，返回结果值
# s = (msvcrt.getch())
# while s != b'a':  # 键是 a
#     s = (msvcrt.getch())
       
requestsss={'name':'李四', 'image': None, 'wait':1}
req = json.dumps(requestsss) #字典数据结构变json(所有程序语言都认识的字符串)
res=requests.post('http://127.0.0.1:6060/', data=req)
while(res.text=='no result'):
    res=requests.post('http://127.0.0.1:6060/', data=req)
    time.sleep(1)
print(res.text)


